package com.qf.controller;

import com.qf.dao.UsersDao;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UsersController {

    @Resource
    private UsersService usersService;

    @GetMapping("/findAll")
    public List<Users> findAll(){
        //System.out.println(usersService.findAll());
        return usersService.findAll();
    }

    @PostMapping("/insert")
    //此处传过来如果是一个对象，需要使用@RequestBody
    public Users insert(@RequestBody Users user){

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        String format = dateFormat.format(new Date());
        //System.out.println(format);
        //Users users = new Users();
        user.setUserId(format);
        /*user.setUserName(user.getUserName());
        user.setPassword(user.getPassword());
        user.setEmail(user.getEmail());
        user.setTel(user.getTel());*/
        user.setLevel(1);
        user.setIntegral(0);
        user.setIfNew(0);

        return usersService.insert(user);
    }

    @PostMapping("/delete/{userId}")
    public String delete(@PathVariable String userId){
        return usersService.delete(userId);
    }

    @PostMapping("/update")
    public Users update(@RequestBody Users user){
        return usersService.update(user);
    }

    @GetMapping("/findOneById/{id}")
    public Users findOneById(@PathVariable String id){
        System.out.println("来了");
        Users oneById = usersService.findOneById(id);
        System.out.println(oneById);
        return oneById;
    }
}
